package DBside;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Tools {
	private static final String TAULabURL = "jdbc:mysql://mysqlsrv.cs.tau.ac.il:3306/DbMysql11";
	private static final String HomeURL="jdbc:mysql://localhost:3305/DbMysql11";
	private static final String userName = "DbMysql11";
	private static final String password = "DbMysql11";
	public static final String LocalURL="jdbc:mysql://localhost:3306/DbMysql11";
	public static final String LocaluserName = "root";
	public static final String Localpassword = "Kaki-152";
	
	private static final int idColumnNumber = 1;
	private static final int nameColumnNumber = 2;
	
	public static void printError(String action, Exception exception){
		System.out.println("fail to " + action + " \n" +
				"Exception:" + exception.toString());
	}
	
	public enum Type{
		VARCHAR("Varchar"),
		DATE("Date"),
		INT("Int"),
		SEARCH("Search"),
		DOUBLE("Double");
		String name;
		
		private Type(String name){
			this.name = name;
		}
		
		public boolean is(Type type) {
			return this.equals(type);
		}
		
		public String getName() {
			return name;
		}
		
		@Override
		public String toString(){
			return this.getName();
		}
	}
	
	public enum Connector{
		AND("AND"),
		OR("OR");
		
		String name;
		
		private Connector(String name){
			this.name = name;
		}
		
		public boolean is(Connector connector) {
			return this.equals(connector);
		}
		
		public String getName(){
			return name;
		}
		
		@Override
		public String toString(){
			return this.getName();
		}
	}
	
	public static String getHomeURL(){
		return HomeURL;
	}
	
	public static String getTauLabURL() {
		return TAULabURL;
	}
	
	public static String getUserName(){
		return userName;
	}
	
	public static String getPassword(){
		return password;
	}
	
	public static int getIdColumnNumber() {
		return idColumnNumber;
	}
	public static int getNameColumnNumber() {
		return nameColumnNumber;
	}
	
	public static Boolean isEmpty(ResultSet resultSet){
		if(resultSet == null){
			return true;
		}
		try {
			if(resultSet.next()){
				resultSet.previous();
				return false;
			}else{
				return true;
			}
		} catch (SQLException e) {
			Tools.printError("isEmpty", e);
			return false;
		}
	}
	
	public enum History{
		ADDEDSONG(1),
		ADDEDFOLLOWER(2),
		CREATEDPLAYLIST(3),
		LISTINTOSONG(4);
		
		private final int typeCode;
		
		private History(int typeCode){
			this.typeCode = typeCode;
		}
		
		public int getTypeCode() {
			return typeCode;
		}
		
		public static History getHistory(int typeCode){
			for(History history : History.values()){
				if(history.getTypeCode() == typeCode){
					return history;
				}
			}
			return null;
		}
	}
	 public static String fixEncoding(String latin1) {
		  try {
		   byte[] bytes = latin1.getBytes("ISO-8859-1");
		   if (!validUTF8(bytes))
		    return latin1;   
		   return new String(bytes, "UTF-8");  
		  } catch (Exception e) {
		   // Impossible, throw unchecked
			  return "";
		   //throw new IllegalStateException("No Latin1 or UTF-8: " + e.getMessage());
		  }

		 }

		 public static boolean validUTF8(byte[] input) {
		  int i = 0;
		  // Check for BOM
		  if (input.length >= 3 && (input[0] & 0xFF) == 0xEF
		    && (input[1] & 0xFF) == 0xBB & (input[2] & 0xFF) == 0xBF) {
		   i = 3;
		  }

		  int end;
		  for (int j = input.length; i < j; ++i) {
		   int octet = input[i];
		   if ((octet & 0x80) == 0) {
		    continue; // ASCII
		   }

		   // Check for UTF-8 leading byte
		   if ((octet & 0xE0) == 0xC0) {
		    end = i + 1;
		   } else if ((octet & 0xF0) == 0xE0) {
		    end = i + 2;
		   } else if ((octet & 0xF8) == 0xF0) {
		    end = i + 3;
		   } else {
		    // Java only supports BMP so 3 is max
		    return false;
		   }

		   while (i < end) {
		    i++;
		    octet = input[i];
		    if ((octet & 0xC0) != 0x80) {
		     // Not a valid trailing byte
		     return false;
		    }
		   }
		  }
		  return true;
		 }
}
